const express=require('express');
const pool=require('../pool.js');

const router=express.Router();



// // 订单列表显示
// router.get('/list',(req,res,next)=>{
//   pool.query('select * from ordering INNER JOIN user ON ordering.user_id=user.uid INNER JOIN room ON ordering.room_id=room.rid,(err,data)=>{
//     if(err){next(err);return;}
//     if (data.length) {
//       res.send({
//         "code":1,
//         "msg":"已查到订单信息",
//         "data":data
//       })
//     } else {
//       res.send({
//         "code":0,
//         "msg":"获取订单信息失败"        
//       })
//     }
//   })
// })



// 订单列表显示(分页查询)
router.get('/list',(req,res,next)=>{
  const page_num = parseInt(req.query.page_num) || 1;
  const page_size = parseInt(req.query.page_size) || 10;
  const params = [(page_num-1)*page_size,page_size];
  var sql = "select * from ordering INNER JOIN user ON ordering.user_id=user.uid INNER JOIN room ON ordering.room_id=room.rid limit ?,?";

  pool.query(sql,params,(err,data)=>{
    if(err){next(err);return;}
    if (data.length) {
      // console.log("data:",data)
      let sqlTotal = "select count(*) as total from ordering"   //统计总数据条数命名为total
      pool.query(sqlTotal,(err,among) => {
        if(err){
          console.log(err);
        } else {
          // console.log(among);
          let total = among[0]['total'];
          res.json({
            "code":1,
            "message":"查询成功",
            "data":data,
            "paging":{
              page_num:page_num,
              page_size:page_size,
              total:total
            }
          })
        }
      })
    } else {
      res.json({
        code:0,
        message:'查询失败'
    })
  }
})
})



// 增加订单
router.post('/add',(req,res,next)=>{
  let obj=req.body;
  pool.query("insert into ordering set ?",[obj],(err,result)=>{
    if(err){
      next(err);
      return;
	  }
    if(result.affectedRows){
      res.send({code:1,msg:'预定成功'});
    }else{     
      res.send({code:0,msg:'预定失败'});
    }
  })
});


//删除订单
router.delete('/del/:oid',(req,res,next)=>{
  //获取数据
  let obj = req.params;
  pool.query('delete from ordering where oid=?',[obj.oid],(err,data)=>{
    //错误处理
    if(err){next(err);return;}
    //查看删除结果
    // console.log(data.affectedRows)
    //data.affectedRows 变化数据条数，删除成功就会有一条变化
    if(data.affectedRows == 0){
      res.send({
        "code":0,
        "msg":"删除失败"
      })
    }else {
       res.send({
        "code":1,
        "msg":"删除成功"
      })
    }
  })
})

//查询单个订单(后台使用)
router.get('/search/:order_num',(req,res,next)=>{
  //获取数据
  let obj = req.params;
  pool.query('select * from ordering where order_num=?',[obj.order_num],(err,data)=>{
    //错误处理
    if(err){next(err);return;}
    
    if(data.length == 0){
      res.send({
        "code":0,
        "msg":"没有查到该订单"
      })
    }else {
       res.send({
        "code":1,
        "msg":"有该订单数据",
        data
      })
    }
  })
})


//个人订单查询（ordering+room多表查询）（用于：预订成功后反馈给用户）
router.get('/:order_num',(req,res,next)=>{
  //获取数据
  let obj = req.params;
  pool.query('select * from ordering INNER JOIN room ON ordering.room_id=room.rid where order_num=?',[obj.order_num],(err,data)=>{
    //错误处理'select * from ordering INNER JOIN room ON ordering.room_id=room.rid where order_num=?
    if(err){next(err);return;} 
    if(data.length == 0){
      res.send({
        "code":0,
        "msg":"没有查到该订单"
      })
    }else {
       res.send({
        "code":1,
        "msg":"有该订单数据",
        data
      })
    }
  })
})


//导出路由器对象
module.exports = router;